package scientistengineer.blogspot.affrpor;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;

public class ListSpeakingPortAff {
	
	private PlayTextListAdapter adapter;
	private ListView listAdapter;
	private int offset_map=0;
	
	private SoundPool soundPool;
	private int volume;
	private Map<Integer, Integer> soundMap;
	
	PlayTextRow [] row_data;
	
	public ListSpeakingPortAff(final Context context, 
			String [] ListPort, String [] ListAffr, String [] rawMp3, final ListView listAdapter)
	{
		row_data = new PlayTextRow[ListPort.length];
		for(int i=0; i<ListPort.length; i++)
		{
			row_data[i] = new PlayTextRow(R.drawable.play_br, ListPort[i], ListAffr[i], R.drawable.play_sa);		
		}
		adapter = new PlayTextListAdapter(context, R.layout.atom_play_translation, row_data);
		//listAdapter = (ListView) findViewById(R.id.greetingsListView);
		listAdapter.setAdapter(adapter);
		
		soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);	
		soundMap = new HashMap<Integer, Integer>();
		for(int i=0; i<rawMp3.length; i++)
		{
			soundMap.put(i, soundPool.load(context, context.getResources().getIdentifier("raw/"+rawMp3[i], "raw", context.getPackageName()), 1));
		}
		offset_map = rawMp3.length/2;
		adapter.setOnAffrClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				final int position = listAdapter.getPositionForView(v);
				soundPool.play(soundMap.get(position), 1, 1, 1, 0, 1f);
			}	
		});
		
		adapter.setOnPortClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				final int position = listAdapter.getPositionForView(v);
				soundPool.play(soundMap.get(position+offset_map), 1, 1, 1, 0, 1f);
			}	
		});	
	}
}
